﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using FbKPI.Model;
namespace FbKPI.Controller
{
    public class UoMController : FbKPI.Controller.dbConnection.ConnectionClass
    {
        public string Save(UoM UoMs)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_InsertUpdateUoM";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = UoMs.ID;
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = UoMs.Description;
            command.Parameters.Add("@FORMAT", SqlDbType.VarChar).Value = UoMs.Format;
            command.Parameters.Add("@ISACTIVE", SqlDbType.VarChar).Value = UoMs.isActive;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = UoMs.LastModifiedBy;
            command.Parameters.Add("@CREATEBY", SqlDbType.VarChar).Value = UoMs.CreateBy;

            return ExecuteNonQuery(command);
        }

        public string Delete(UoM UoMs)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_DeleteUoM";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = UoMs.ID;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = UoMs.LastModifiedBy;

            return ExecuteNonQuery(command);
        }


        public string BeforeDelete(UoM UoMs)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_CheckBeforeDeleteUOM";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = UoMs.ID;
            

            return GetSingleData(command);
        }

        public IList<UoM> GetAll(string sSearch, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllUoMs";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;

            DataSet dx = GetTable(command);

            List<UoM> UoMs = new List<UoM>();

            switch (SortDirection)
            {
                case "asc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description ASC";
                            break;
                        case "Format":
                            dx.Tables[0].DefaultView.Sort = "Format ASC";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate ASC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy ASC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate ASC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy ASC";
                            break;
                    }
                    break;
                case "desc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description DESC";
                            break;
                        case "Format":
                            dx.Tables[0].DefaultView.Sort = "Format Desc";
                            break;
                        case "LastModifiedDate":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedDate DESC";
                            break;
                        case "LastModifiedBy":
                            dx.Tables[0].DefaultView.Sort = "LastModifiedBy DESC";
                            break;
                        case "CreateDate":
                            dx.Tables[0].DefaultView.Sort = "CreateDate DESC";
                            break;
                        case "CreateBy":
                            dx.Tables[0].DefaultView.Sort = "CreateBy DESC";
                            break;
                    }
                    break;
            }

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        UoM p = new UoM();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.Format = (UoMFormat)Enum.Parse(typeof(UoMFormat), dt.Rows[i]["Format"].ToString());
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());                        
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        UoMs.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return UoMs.ToList();
        }

        public UoM GetUoMByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetUoMByID";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            IList<string> Data = GetSingleRow(command);
            UoM UoMs = null;

            if (Data != null && Data.Count > 0)
            {
                UoMs = new UoM();
                UoMs.ID = Convert.ToInt16(Data[0]);
                UoMs.Description = Data[1].ToString();
                UoMs.Format = (UoMFormat)Enum.Parse(typeof(UoMFormat), Data[2].ToString());
                UoMs.isActive = Convert.ToBoolean(Data[3]);
                UoMs.LastModifiedDate = Data[4] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[4].ToString());
                UoMs.LastModifiedBy = Data[5].ToString();
                UoMs.CreateDate = Data[6] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[6].ToString());
                UoMs.CreateBy = Data[7].ToString();
            }

            return UoMs;
        }

        public UoM GetUoMByDescription(string Description)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetUoMByDescription";
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = Description;

            IList<string> Data = GetSingleRow(command);
            UoM UoMs = null;

            if (Data != null && Data.Count > 0)
            {
                UoMs = new UoM();
                UoMs.ID = Convert.ToInt16(Data[0]);
                UoMs.Description = Data[1].ToString();
                UoMs.Format = (UoMFormat)Enum.Parse(typeof(UoMFormat), Data[2].ToString());
                UoMs.isActive = Convert.ToBoolean(Data[3]);
                UoMs.LastModifiedDate = Data[4] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[4].ToString());
                UoMs.LastModifiedBy = Data[5].ToString();
                UoMs.CreateDate = Data[6] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[6].ToString());
                UoMs.CreateBy = Data[7].ToString();
            }

            return UoMs;
        }
    }
}